package redis

import (
	"context"
	"time"

	"github.com/go-redis/redis/v8"
)


type Cacher interface{
	SetUrl(ctx context.Context,key,value string,duration time.Duration) error
	GetUrl(ctx context.Context,key string) (string,error)
}

type RedisCache struct{
	client *redis.Client
}
func NewRedisCache(addr string,password string, DB,PoolSize,MinIdleConns int ) (*RedisCache,error){
	client := redis.NewClient(&redis.Options{
			Addr: addr,
			Password: password,
			DB: DB,                                      // 使用默认 DB
			// 连接池配置
			PoolSize:    PoolSize ,     // 连接池大小
			MinIdleConns: MinIdleConns, // 最小空闲连接数
		})
	if err := client.Ping(context.Background()).Err();err != nil{
		return nil,err
	}
	return &RedisCache{
		client: client,
	},nil
}
func (r *RedisCache) SetUrl(ctx context.Context,key,value string,duration time.Duration) error{
	return r.client.Set(ctx,key,value,duration).Err()
}
func (r *RedisCache) GetUrl(ctx context.Context,key string) (string,error){
	return r.client.Get(ctx,key).Result()
}